package app.eeui.umeng.ui.module;

import android.os.Handler;
import android.os.Looper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.HashMap;
import java.util.Map;

import app.eeui.framework.extend.base.WXModuleBase;

public class eeuiUmengShareModule extends WXModuleBase {

    @JSMethod
    public void webShare(String options,final JSCallback callback) {

        JSONObject opts = JSON.parseObject(options);
        UMWeb web =new UMWeb(opts.getString("url"));
        web.setTitle(opts.getString("title"));//标题
        UMImage umImage = new UMImage(getContext(),opts.getString("thumb"));
        umImage.setDescription(opts.getString("description"));
        umImage.setTitle(opts.getString("description"));
        web.setThumb(umImage);//缩略图
        web.setDescription(opts.getString("description"));//描述
        UMShareListener shareListener =new UMShareListener(){
            /**
             * @descrption 分享开始的回调
             * @param platform 平台类型
             */
            @Override
            public void onStart(SHARE_MEDIA platform){
                Map<String,Object> data = new HashMap<>();
                data.put("type","success");
                data.put("content","分享成功");
                callback.invoke(data);
            }

            /**
             * @descrption 分享成功的回调
             * @param platform 平台类型
             */
            @Override
            public void onResult(SHARE_MEDIA platform){

            }

            /**
             * @descrption 分享失败的回调
             * @param platform 平台类型
             * @param t 错误原因
             */
            @Override
            public void onError(SHARE_MEDIA platform,Throwable t){
                Map<String,Object> data = new HashMap<>();
                data.put("type","error");
                data.put("content","分享失败");
                callback.invoke(data);
            }

            /**
             * @descrption 分享取消的回调
             * @param platform 平台类型
             */
            @Override
            public void onCancel(SHARE_MEDIA platform){
                Map<String,Object> data = new HashMap<>();
                data.put("type","cancel");
                data.put("content","取消分享");
                callback.invoke(data);
            }
        };

        new ShareAction(getActivity()).withMedia(web).setDisplayList(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.WEIXIN_FAVORITE)
                .setCallback(shareListener).open();
    }

}
